#include "ftbrowser.h"
#include "ftvboxlayout.h"
#include "ftpushbutton.h"
#include "ftcontrols.h"


FTBrowser::FTBrowser(const QString& url) :
    RotatingDialog()
{
    initialize( QUrl(url) );
}

FTBrowser::FTBrowser(const QUrl& url) :
    RotatingDialog()
{
    initialize( url );
}

void FTBrowser::initialize(const QUrl & url )
{
    load_progress = new QProgressBar();
    load_progress->setMinimum(0);
    load_progress->setMaximum(100);
    load_progress->hide();
    QFont helpFont = FTControls::font( FTControls::FontNormal );

    webView = new QWebView();
    flick.activateOn( webView );
    webView->setStyleSheet(QString("background-color:rgb(150,147,88);")+
                           QString("padding: 7px ;")+
                           QString("font-size: %1px; ").arg( (int)((double)helpFont.pixelSize()) ) +
#ifdef Q_OS_ANDROID
                             QString("font-style: italic; ") +
#endif
                           QString("color:rgb(255,255,255);") );

    connect( webView, SIGNAL(loadStarted()),
             this, SLOT(loadStarted()) );
    connect( webView, SIGNAL(loadProgress(int)),
             this, SLOT(loadProgress(int)) );
    connect( webView, SIGNAL(loadFinished(bool)),
             this, SLOT(loadFinished(bool)) );
    webView->load( url );
    webView->show();

    closeButton = new FTPushButton( tr("Close Browser") );
    connect( closeButton, SIGNAL(clicked()),
             this, SLOT(reject()) );
}

void FTBrowser::loadStarted()
{
    int dh = FTControls::defaultHeight();
    webView->setEnabled(false);
    load_progress->move(0,  (height() - load_progress->height())/2);
    load_progress->resize(width(), dh);
    load_progress->setValue(0);
    load_progress->show();
}

void FTBrowser::loadProgress(int p)
{
    load_progress->setValue(p);
}

void FTBrowser::loadFinished(bool)
{
    load_progress->hide();
    webView->setEnabled(true);
}

QLayout* FTBrowser::createVertical()
{
    FTVBoxLayout* grid = new FTVBoxLayout();

    grid->addWidget( webView );
    grid->addWidget( load_progress );
    grid->addWidget( closeButton );

    return grid;
}
